# 沙河虚拟机上
#!/bin/bash
# 开启防火墙端口
sudo iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 6443 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 9092 -j ACCEPT
# 保存配置
sudo iptables-save
swapoff -a
sudo kubectl -n kubesphere-system patch svc ks-apiserver -p '{"spec":{"type":"NodePort","ports":[{"name":"ks-apiserver","port":80,"protocal":"TCP","targetPort":9090,"nodePort":30881}]}}'

# 定义要检查的端口列表
port_list=("8000" "8001" "9092")

# 检查端口是否被占用
check_port() {
  local port="$1"
  local port_status=$(netstat -tuln | awk -v p="$port" '$4 ~ (":"p"$") {print $4}')
  if [[ -n "$port_status" ]]; then
    echo "Port $port is already in use."
    return 0
  else
    echo "Port $port is available."
    return 1
  fi
}

# 查找占用指定端口的进程ID
find_process_id() {
  local port="$1"
  local process_id=$(netstat -tuln | awk -v p="$port" '$4 ~ (":"p"$") {print $NF}' | sed 's/[^0-9]*//g')
  echo "$process_id"
}

# 终止指定进程
kill_process() {
  local process_id="$1"
  if [[ -n "$process_id" ]]; then
    echo "Killing process $process_id"
    kill -9 "$process_id"
    return 0
  else
    echo "No process is using port $port."
    return 1
  fi
}

# 遍历端口列表，逐个检查端口是否被占用
for port in "${port_list[@]}"; do
  check_port "$port"
  if [[ $? -eq 0 ]]; then
    process_id=$(find_process_id "$port")
    kill_process "$process_id"
  fi
done

# shell语句中并行运行命令
cd /home/nuc/edge-system && nohup yarn dev:server &
cd /home/nuc/edge-system && nohup yarn dev:client </dev/null >stdout.log 2>stderr.log &
cd /home/nuc/mec-backend && nohup java -jar kube_platform-2.6.3.jar > cjk.log &
wait

